{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>{$title}</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" lay-filter="form">

        <div class="layui-form-item">
            <label class="layui-form-label">{:lang('teacher_no')}</label>
            <div class="layui-input-4">
                <input type="text" name="teacher_no" lay-verify="required" placeholder="{:lang('pleaseEnter')}编号" class="layui-input teacher_no">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">{:lang('username')}</label>
            <div class="layui-input-4">
                <input type="text" name="user_name" lay-verify="required" placeholder="{:lang('pleaseEnter')}登录用户名" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                用户名在4到25个字符之间。
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">{:lang('pwd')}</label>
            <div class="layui-input-4">
                <input type="password" name="password" placeholder="{:lang('pleaseEnter')}登录密码" {if condition="ACTION_NAME eq 'teacherAdd'"}lay-verify="required"{/if} class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                密码必须大于6位，小于15位。
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">{:lang('true_name')}</label>
            <div class="layui-input-4">
                <input type="text" name="true_name"  value="" lay-verify="required" placeholder="{:lang('pleaseEnter')}{:lang('true_name')}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">{:lang('sex')}</label>
            <div class="layui-input-4">
                <div class="layui-input-inline">
                    <input type="radio" name="sex" value="1" title="男">
                    <input type="radio" name="sex" value="2" title="女" checked>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">{:lang('tel')}</label>
            <div class="layui-input-4">
                <input type="text" name="mobile" lay-verify="phone" value="" placeholder="{:lang('pleaseEnter')}{:lang('tel')}" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                用于密码找回，请认真填写。
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">{:lang('section')}</label>
            <div class="layui-input-inline">
                <select name="section_id" lay-filter="section_id" lay-verify="required">
                    <option value="" disabled="">请选择教师部门</option>
                    {volist name="sectionGroup" id="vo"}
                    <option value="{$vo.section_id}">{$vo.title}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="position">
            <div class="layui-form-item">
                <label class="layui-form-label">{:lang('position')}</label>
                <div class="layui-input-inline">
                    <select lay-filter="position_id" lay-verify="required">
                        <option value="0">请选择任职</option>
                        {volist name="positionGroup" id="vo"}
                        <option value="{$vo.position_id}" name="{$vo.position_name}">{$vo.position_name}</option>
                        {/volist}
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">{:lang('degree')}</label>
            <div class="layui-input-4">
                <input type="text" name="degree"  lay-verify="required" value="" placeholder="{:lang('pleaseEnter')}{:lang('degree')}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">{:lang('graduation_school')}</label>
            <div class="layui-input-4">
                <input type="text" name="graduation_school" lay-verify="required"  value="" placeholder="{:lang('pleaseEnter')}{:lang('graduation_school')}" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="hidden" name="id">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">{:lang('submit')}</button>
                <a href="{:url('lists')}" class="layui-btn layui-btn-primary">{:lang('back')}</a>
            </div>
        </div>
    </form>
</div>
{include file="common/foot"/}
<script>
    layui.use(['form', 'layer','upload'], function () {
        var form = layui.form, layer = layui.layer,$= layui.jquery,upload = layui.upload;
        var info = {$info|raw};
        console.log(info);
        form.val("form", info);
        /*if(info){
            $('#adPic').attr('src',info.avatar);
        }*/
        form.render();

        $(".teacher_no").blur(function(){
            var teacher_no = $('.teacher_no').val();
            $.post("{:url('detection')}",{teacher_no:teacher_no},function(res){
                if(res.code==0){
                    layer.msg(res.msg,{time:3000,icon:2});
                    $('.teacher_no').focus();
                    return;
                }
            });
        });

        //职务操作
        form.on('select(position_id)', function(data){
            position_id = data.value;
            if((position_id!=0)&&($("#p_item"+position_id).attr("class")==undefined)){

                position_name = data.elem[data.elem.selectedIndex].text;

                var item = "<div class=\"layui-form-item\" id='p_item"+position_id+"'>\n" +
                    "            <label class=\"layui-form-label\">{:lang('position')}</label>\n" +
                    "            <div class=\"layui-input-inline\">\n" +
                    "                <input type=\"text\" value='"+position_name+"' class=\"layui-input\" readOnly=\"true\" >\n" +
                    "            </div>\n" +
                    "            <input type=\"radio\" name=\"position_id\" value='1' position_id='"+position_id+"' title=\"主职务\" id='position_radio"+position_id+"'>\n"+
                    "            <button class=\"layui-btn layui-btn-primary romove layui-btn-sm\" position_id='"+position_id+"'>移除</button>\n"+
                    "        </div>";

                $(".position").after(item);
                form.render();
            }

        });

        //移除元素
        $(document).on('click','.romove',function(){
            $("#p_item"+$(this).attr('position_id')).remove();
        });

        form.on('submit(submit)', function (data) {

            var duty= $('input[id^="position_radio"]');

            var numArr = {}; // 定义一个空对象

            for (var i = 0; i < duty.length; i++) {
                var position_id = duty.eq(i).attr('position_id');

                if(duty.eq(i)[0].checked){
                    numArr[position_id] = 1;
                }else{
                    numArr[position_id] = 0;
                }

            }
            data.field.position_id = numArr;
            loading =layer.load(1, {shade: [0.1,'#fff']});

            $.post("", data.field, function (res) {
                layer.close(loading);
                if (res.code > 0) {
                    layer.msg(res.msg, {time: 1800, icon: 1}, function () {
                        location.href = res.url;
                    });
                } else {
                    layer.msg(res.msg, {time: 1800, icon: 2});
                }
            });
        });
        /* //普通图片上传
         var uploadInst = upload.render({
             elem: '#adBtn'
             ,url: '{:url("UpFiles/upload")}'
             ,before: function(obj){
                 //预读本地文件示例，不支持ie8
                 obj.preview(function(index, file, result){
                     $('#adPic').attr('src', result); //图片链接（base64）
                 });
             },
             done: function(res){
                 if(res.code>0){
                     $('#avatar').val(res.url);
                 }else{
                     //如果上传失败
                     return layer.msg('上传失败');
                 }
             }
             ,error: function(){
                 //演示失败状态，并实现重传
                 var demoText = $('#demoText');
                 demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                 demoText.find('.demo-reload').on('click', function(){
                     uploadInst.upload();
                 });
             }
         });*/

    });

</script>